<?
require_once('functions.php');

function GetUsers()
{
        $conn = GetConnection();
        return $conn->query('SELECT * FROM 2012WP_Users');
}
function BlankUser()
{
        return array('FirstName'=>null,'LastName'=>null, 'PhoneNumber'=>null, 'Email'=>null, 'UserName'=>null, 'Password'=>null, 'UserType_id'=>null, 'PaymentMethods_id'=>null, 'created_at'=>null,'updated_at'=>null,'id'=>null);
}
function GetUser($id)
{
        $conn = GetConnection();
        $sql = "SELECT * FROM 2012WP_Users WHERE id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $row = $results->fetch_assoc();
        $conn->close();
        return $row;
}
function SaveUser(&$row)
{
        $conn = GetConnection();
        $row2 = EscapeRow($row, $conn);
        if(empty($row['id']))
        {
                $sql =  "Insert 2012WP_Users (FirstName, LastName, PhoneNumber, Email, UserName, Password, 2012WP_UserType_id, 2012WP_PaymentMethods_id, created_at) "
                        .       " Values ('$row2[FirstName]','$row2[LastName]', '$row2[PhoneNumber]', '$row2[Email]', '$row2[UserName]', '$row2[Password]', '$row2[UserType_id]', '$row2[PaymentMethods_id]', NOW() ) ";
        }else{
                $sql =  "UPDATE 2012WP_Users "
                        .       "Set FirstName='$row2[FirstName]', LastName='$row2[LastName]', PhoneNumber='$row2[PhoneNumber]', Email='$row2[Email]', UserName='$row2[UserName]', Password='$row2[Password]', 2012WP_UserType_id='$row2[UserType_id]', 2012WP_PaymentMethods_id='$row2[PaymentMethods_id]' "
                        .       "WHERE id=$row2[id] ";
        }
        $results = $conn->query($sql);
        //echo $sql;
        $error = $conn->error;
        if(empty($row['id']) && empty($error))
        {
                $row['id'] = $conn->insert_id;
        }
        $conn->close();
       
        return $error == '' ? true : array('SQL Error' => $error);
}
function DeleteUser($id)
{
        $conn = GetConnection();
        $sql =  "DELETE FROM 2012WP_Users WHERE id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $error = $conn->error;
        $conn->close();
       
        return $error == '' ? true : array('SQL Error' => $error);
}
function ValidateUser($row)
{
        $errors = array();
        if(empty($row['FirstName'])) $errors['FirstName'] = 'First Name is required';
        if(empty($row['LastName'])) $errors['LastName'] = 'Last Name is required';
		if(empty($row['PhoneNumber'])) $errors['PhoneNumber'] = 'Phone Number is required';
		if(empty($row['Email'])) $errors['Email'] = 'Email is required';
		if(empty($row['UserName'])) $errors['UserName'] = 'User Name is required';
		if(empty($row['Password'])) $errors['Password'] = 'Password is required';
        if(!is_numeric($row['UserType_id'])) $errors['UserType_id'] = 'UserType_id must be a number';
        if(empty($row['UserType_id'])) $errors['UserType_id'] = 'UserType_id is required';
		if(!is_numeric($row['PaymentMethods_id'])) $errors['PaymentMethods_id'] = 'PaymentMethods_id must be a number';
        if(empty($row['PaymentMethods_id'])) $errors['PaymentMethods_id'] = 'PaymentMethods_id is required';
       
        return count($errors) == 0 ? true : $errors;
}
